package dame;

import java.util.Arrays;

public class MyStack {      //数组实现栈
    public int[] elem;
    public int usesize;
    public  MyStack(){
        this.elem = new int[10];
    }
    public void push(int val){  //入栈
        if (isFill()){
            this.elem = Arrays.copyOf(elem,2*elem.length);  //扩容
        }
        elem[usesize] = val;
        usesize++;
    }
    private boolean isFill(){ //判断是否满
        return elem.length==usesize;
    }
    public int pop(){  //出栈
        if (isempty()){
            try {
                throw new Emptyisstack("栈为空");
            }catch (Emptyisstack e){
                e.printStackTrace();
            }
        }
        int val= this.elem[usesize-1];
        usesize--;
        return val;
    }
    public int peek(){  //获取栈顶元素
        if (isempty()){
            try {
                throw new Emptyisstack("栈为空");
            }catch (Emptyisstack e){
                e.printStackTrace();
            }
        }
        return this.elem[usesize-1];
    }
    public boolean isempty() {   //检查栈是否为空
        return usesize == 0;
    }
}
